<template>
  <div class="statistics-page">
    <!-- 面包屑导航 -->
    <a-breadcrumb class="page-breadcrumb">
      <a-breadcrumb-item>
        <router-link to="/statistics">统计分析中心</router-link>
      </a-breadcrumb-item>
      <a-breadcrumb-item>生死数据统计</a-breadcrumb-item>
      <a-breadcrumb-item>死因分类统计</a-breadcrumb-item>
    </a-breadcrumb>

    <!-- 页面标题 -->
    <div class="dashboard-header">
      <div class="header-content">
        <div class="ancient-title">死因分类统计</div>
        <div class="header-subtitle">阴曹地府 · 生死档案分析</div>
      </div>
      <div class="header-decoration">
        <div class="decoration-line"></div>
        <div class="decoration-symbol">⚊</div>
        <div class="decoration-line"></div>
      </div>
    </div>

    <!-- 核心指标卡片 -->
    <div class="stats-section">
      <a-row :gutter="[20, 20]" class="stats-grid">
        <a-col :xs="24" :sm="12" :xl="6">
          <div class="stat-card disease-stat">
            <div class="stat-icon">
              <MedicineBoxOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-title">疾病死亡</div>
              <div class="stat-value">{{ diseaseDeaths }}</div>
              <div class="stat-label">占比 {{ diseasePercentage }}%</div>
            </div>
          </div>
        </a-col>

        <a-col :xs="24" :sm="12" :xl="6">
          <div class="stat-card accident-stat">
            <div class="stat-icon">
              <CarOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-title">意外事故</div>
              <div class="stat-value">{{ accidentDeaths }}</div>
              <div class="stat-label">占比 {{ accidentPercentage }}%</div>
            </div>
          </div>
        </a-col>

        <a-col :xs="24" :sm="12" :xl="6">
          <div class="stat-card natural-stat">
            <div class="stat-icon">
              <HeartOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-title">自然死亡</div>
              <div class="stat-value">{{ naturalDeaths }}</div>
              <div class="stat-label">占比 {{ naturalPercentage }}%</div>
            </div>
          </div>
        </a-col>

        <a-col :xs="24" :sm="12" :xl="6">
          <div class="stat-card other-stat">
            <div class="stat-icon">
              <QuestionCircleOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-title">其他原因</div>
              <div class="stat-value">{{ otherDeaths }}</div>
              <div class="stat-label">占比 {{ otherPercentage }}%</div>
            </div>
          </div>
        </a-col>
      </a-row>
    </div>

    <!-- 主要图表区域 -->
    <div class="main-content">
      <a-row :gutter="[32, 32]" class="content-grid">
        <!-- 死因分布饼图 -->
        <a-col :xs="24" :lg="12">
          <a-card title="死因分布统计" :bordered="false" class="chart-card">
            <template #extra>
              <a-select v-model:value="pieChartType" style="width: 120px">
                <a-select-option value="total">总体统计</a-select-option>
                <a-select-option value="monthly">本月统计</a-select-option>
                <a-select-option value="yearly">本年统计</a-select-option>
              </a-select>
            </template>
            <div class="chart-container">
              <v-chart :option="deathCausePieOption" style="height: 400px" />
            </div>
          </a-card>
        </a-col>

        <!-- 疾病死因详细分类 -->
        <a-col :xs="24" :lg="12">
          <a-card title="疾病死因详细分类" :bordered="false" class="chart-card">
            <div class="chart-container">
              <v-chart :option="diseaseDetailOption" style="height: 400px" />
            </div>
          </a-card>
        </a-col>

        <!-- 年龄组死因分析 -->
        <a-col :xs="24" :lg="12">
          <a-card title="年龄组死因分析" :bordered="false" class="chart-card">
            <div class="chart-container">
              <v-chart :option="ageGroupOption" style="height: 400px" />
            </div>
          </a-card>
        </a-col>

        <!-- 死因趋势变化 -->
        <a-col :xs="24" :lg="12">
          <a-card title="死因趋势变化" :bordered="false" class="chart-card">
            <template #extra>
              <a-range-picker v-model:value="trendDateRange" />
            </template>
            <div class="chart-container">
              <v-chart :option="trendOption" style="height: 400px" />
            </div>
          </a-card>
        </a-col>

        <!-- 地区死因对比 -->
        <a-col :xs="24">
          <a-card title="地区死因对比" :bordered="false" class="chart-card">
            <div class="chart-container">
              <v-chart :option="regionComparisonOption" style="height: 500px" />
            </div>
          </a-card>
        </a-col>

        <!-- 死因详细数据表 -->
        <a-col :xs="24" :lg="16">
          <a-card title="死因详细统计表" :bordered="false" class="table-card">
            <a-table :columns="deathCauseColumns" :data-source="deathCauseTableData" :pagination="{ pageSize: 10 }" size="small" />
          </a-card>
        </a-col>

        <!-- 死因预警分析 -->
        <a-col :xs="24" :lg="8">
          <a-card title="死因预警分析" :bordered="false" class="warning-card">
            <div class="warning-list">
              <div v-for="warning in warningList" :key="warning.id" class="warning-item">
                <div class="warning-header">
                  <span :class="['warning-level', warning.level]">{{ warning.level }}</span>
                  <span class="warning-title">{{ warning.title }}</span>
                </div>
                <div class="warning-content">{{ warning.content }}</div>
                <div class="warning-time">{{ warning.time }}</div>
              </div>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import dayjs from 'dayjs'
import { MedicineBoxOutlined, CarOutlined, HeartOutlined, QuestionCircleOutlined } from '@ant-design/icons-vue'
import VChart from 'vue-echarts'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart, BarChart, LineChart } from 'echarts/charts'
import { TitleComponent, TooltipComponent, LegendComponent, GridComponent } from 'echarts/components'

// 注册 ECharts 组件
use([CanvasRenderer, PieChart, BarChart, LineChart, TitleComponent, TooltipComponent, LegendComponent, GridComponent])

// 响应式数据
const loading = ref(false)
const pieChartType = ref('total')
// 修复日期选择器格式
const trendDateRange = ref([dayjs('2024-01-01'), dayjs('2024-12-31')])

// 核心指标数据
const diseaseDeaths = ref('127,485')
const diseasePercentage = ref('45.8')
const accidentDeaths = ref('68,254')
const accidentPercentage = ref('24.5')
const naturalDeaths = ref('52,187')
const naturalPercentage = ref('18.7')
const otherDeaths = ref('30,658')
const otherPercentage = ref('11.0')

// 死因饼图配置
const deathCausePieOption = computed(() => {
  const data = [
    { value: 127485, name: '疾病死亡', itemStyle: { color: '#ff7875' } },
    { value: 68254, name: '意外事故', itemStyle: { color: '#ffa940' } },
    { value: 52187, name: '自然死亡', itemStyle: { color: '#73d13d' } },
    { value: 30658, name: '其他原因', itemStyle: { color: '#40a9ff' } }
  ]

  return {
    title: {
      text: '死因分布',
      left: 'center',
      textStyle: { color: '#d4990a', fontSize: 16 }
    },
    tooltip: {
      trigger: 'item',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
      textStyle: { color: '#fff' },
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      top: 'center',
      textStyle: { color: '#fff' }
    },
    series: [
      {
        name: '死因分布',
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['60%', '50%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold',
            color: '#fff'
          }
        },
        labelLine: {
          show: false
        },
        data: data
      }
    ]
  }
})

// 疾病详细分类图表
const diseaseDetailOption = computed(() => {
  const categories = ['心血管疾病', '癌症', '呼吸系统', '消化系统', '神经系统', '感染性疾病', '其他疾病']
  const data = [35420, 28765, 19843, 15672, 12387, 9584, 5814]

  return {
    title: {
      text: '疾病死因细分',
      left: 'center',
      textStyle: { color: '#d4990a', fontSize: 16 }
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
      textStyle: { color: '#fff' }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: categories,
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999', rotate: 45 }
    },
    yAxis: {
      type: 'value',
      name: '死亡人数',
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999' },
      splitLine: { lineStyle: { color: '#333' } }
    },
    series: [
      {
        name: '死亡人数',
        type: 'bar',
        data: data,
        itemStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              { offset: 0, color: '#ff7875' },
              { offset: 1, color: '#ff4d4f' }
            ]
          }
        },
        emphasis: {
          itemStyle: {
            color: '#ff7875'
          }
        }
      }
    ]
  }
})

// 年龄组死因分析
const ageGroupOption = computed(() => {
  const ageGroups = ['0-10', '11-20', '21-30', '31-40', '41-50', '51-60', '61-70', '71-80', '80+']

  return {
    title: {
      text: '年龄组死因分析',
      left: 'center',
      textStyle: { color: '#d4990a', fontSize: 16 }
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
      textStyle: { color: '#fff' }
    },
    legend: {
      data: ['疾病', '意外', '自然', '其他'],
      top: 30,
      textStyle: { color: '#fff' }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ageGroups,
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999' }
    },
    yAxis: {
      type: 'value',
      name: '死亡人数',
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999' },
      splitLine: { lineStyle: { color: '#333' } }
    },
    series: [
      {
        name: '疾病',
        type: 'bar',
        stack: 'total',
        data: [1200, 800, 1500, 3200, 8500, 18000, 28000, 35000, 30785],
        itemStyle: { color: '#ff7875' }
      },
      {
        name: '意外',
        type: 'bar',
        stack: 'total',
        data: [2500, 3200, 8500, 12000, 15000, 12000, 8000, 4000, 2054],
        itemStyle: { color: '#ffa940' }
      },
      {
        name: '自然',
        type: 'bar',
        stack: 'total',
        data: [100, 50, 200, 500, 1200, 3500, 8000, 15000, 23637],
        itemStyle: { color: '#73d13d' }
      },
      {
        name: '其他',
        type: 'bar',
        stack: 'total',
        data: [500, 800, 1200, 2000, 3500, 5000, 6000, 7000, 4658],
        itemStyle: { color: '#40a9ff' }
      }
    ]
  }
})

// 死因趋势图表
const trendOption = computed(() => {
  const months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']

  return {
    title: {
      text: '死因月度趋势',
      left: 'center',
      textStyle: { color: '#d4990a', fontSize: 16 }
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
      textStyle: { color: '#fff' }
    },
    legend: {
      data: ['疾病死亡', '意外事故', '自然死亡', '其他原因'],
      top: 30,
      textStyle: { color: '#fff' }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: months,
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999' }
    },
    yAxis: {
      type: 'value',
      name: '死亡人数',
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999' },
      splitLine: { lineStyle: { color: '#333' } }
    },
    series: [
      {
        name: '疾病死亡',
        type: 'line',
        smooth: true,
        data: [12000, 11500, 10800, 10200, 9800, 9200, 8800, 9100, 9800, 10500, 11200, 12500],
        lineStyle: { color: '#ff7875', width: 3 },
        areaStyle: { color: 'rgba(255, 120, 117, 0.3)' }
      },
      {
        name: '意外事故',
        type: 'line',
        smooth: true,
        data: [6200, 5800, 5500, 5200, 5800, 6200, 6800, 6500, 6000, 5700, 5900, 6300],
        lineStyle: { color: '#ffa940', width: 3 },
        areaStyle: { color: 'rgba(255, 169, 64, 0.3)' }
      },
      {
        name: '自然死亡',
        type: 'line',
        smooth: true,
        data: [4800, 4600, 4200, 3800, 3500, 3200, 2900, 3100, 3600, 4100, 4400, 4900],
        lineStyle: { color: '#73d13d', width: 3 },
        areaStyle: { color: 'rgba(115, 209, 61, 0.3)' }
      },
      {
        name: '其他原因',
        type: 'line',
        smooth: true,
        data: [2800, 2600, 2400, 2200, 2100, 2000, 1900, 2000, 2200, 2500, 2700, 2900],
        lineStyle: { color: '#40a9ff', width: 3 },
        areaStyle: { color: 'rgba(64, 169, 255, 0.3)' }
      }
    ]
  }
})

// 地区对比图表
const regionComparisonOption = computed(() => {
  const regions = ['华北地区', '东北地区', '华东地区', '华中地区', '华南地区', '西北地区', '西南地区']

  return {
    title: {
      text: '各地区死因分布对比',
      left: 'center',
      textStyle: { color: '#d4990a', fontSize: 16 }
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
      textStyle: { color: '#fff' }
    },
    legend: {
      data: ['疾病死亡', '意外事故', '自然死亡', '其他原因'],
      top: 30,
      textStyle: { color: '#fff' }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: regions,
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999', rotate: 45 }
    },
    yAxis: {
      type: 'value',
      name: '死亡人数',
      axisLine: { lineStyle: { color: '#666' } },
      axisLabel: { color: '#999' },
      splitLine: { lineStyle: { color: '#333' } }
    },
    series: [
      {
        name: '疾病死亡',
        type: 'bar',
        data: [22000, 15000, 28000, 18000, 16000, 12000, 16485],
        itemStyle: { color: '#ff7875' }
      },
      {
        name: '意外事故',
        type: 'bar',
        data: [12000, 8000, 15000, 9000, 8500, 7500, 8254],
        itemStyle: { color: '#ffa940' }
      },
      {
        name: '自然死亡',
        type: 'bar',
        data: [8000, 6000, 12000, 7000, 6500, 5500, 7187],
        itemStyle: { color: '#73d13d' }
      },
      {
        name: '其他原因',
        type: 'bar',
        data: [5000, 3500, 7000, 4000, 3800, 3200, 4158],
        itemStyle: { color: '#40a9ff' }
      }
    ]
  }
})

// 表格数据
const deathCauseColumns = [
  { title: '死因分类', dataIndex: 'category', key: 'category', width: 120 },
  { title: '具体原因', dataIndex: 'cause', key: 'cause' },
  { title: '死亡人数', dataIndex: 'count', key: 'count', width: 100, sorter: (a, b) => a.count - b.count },
  { title: '占比', dataIndex: 'percentage', key: 'percentage', width: 80 },
  { title: '同比增长', dataIndex: 'growth', key: 'growth', width: 100 },
  { title: '风险等级', dataIndex: 'risk', key: 'risk', width: 100 }
]

const deathCauseTableData = ref([
  { key: 1, category: '心血管疾病', cause: '心肌梗死', count: 18500, percentage: '14.5%', growth: '+2.3%', risk: '高风险' },
  { key: 2, category: '心血管疾病', cause: '脑卒中', count: 16920, percentage: '13.3%', growth: '+1.8%', risk: '高风险' },
  { key: 3, category: '癌症', cause: '肺癌', count: 12340, percentage: '9.7%', growth: '+3.1%', risk: '极高' },
  { key: 4, category: '癌症', cause: '胃癌', count: 8765, percentage: '6.9%', growth: '+0.9%', risk: '高风险' },
  { key: 5, category: '交通事故', cause: '车祸', count: 25680, percentage: '20.1%', growth: '-1.2%', risk: '中风险' },
  { key: 6, category: '意外溺水', cause: '溺水', count: 8934, percentage: '7.0%', growth: '+4.5%', risk: '中风险' },
  { key: 7, category: '自然衰老', cause: '器官衰竭', count: 35420, percentage: '27.8%', growth: '+1.1%', risk: '低风险' },
  { key: 8, category: '自然灾害', cause: '地震', count: 2156, percentage: '1.7%', growth: '+15.3%', risk: '不可控' },
  { key: 9, category: '呼吸系统', cause: '肺炎', count: 9876, percentage: '7.7%', growth: '+2.8%', risk: '中风险' },
  { key: 10, category: '消化系统', cause: '肝硬化', count: 6543, percentage: '5.1%', growth: '+0.5%', risk: '中风险' }
])

// 预警数据
const warningList = ref([
  {
    id: 1,
    level: '紧急',
    title: '心血管疾病死亡率上升',
    content: '本月心血管疾病死亡人数较上月增长15.3%，需要重点关注。',
    time: '2小时前'
  },
  {
    id: 2,
    level: '警告',
    title: '交通事故死亡率异常',
    content: '华东地区交通事故死亡率出现异常波动，建议加强监管。',
    time: '5小时前'
  },
  {
    id: 3,
    level: '提醒',
    title: '呼吸系统疾病增长',
    content: '冬季呼吸系统疾病死亡率预计将上升，提前做好准备。',
    time: '1天前'
  },
  {
    id: 4,
    level: '信息',
    title: '数据统计完成',
    content: '11月份死因统计分析报告已生成，可查看详细数据。',
    time: '2天前'
  }
])

onMounted(() => {
  loading.value = false
})
</script>

<style scoped>
.statistics-page {
  padding: 24px;
  min-height: 100vh;
}

.page-breadcrumb {
  margin-bottom: 16px;
}

.page-breadcrumb :deep(.ant-breadcrumb-link) {
  color: #d4990a;
}

.dashboard-header {
  text-align: center;
  margin-bottom: 32px;
}

.header-content {
  margin-bottom: 16px;
}

.ancient-title {
  font-size: 32px;
  font-weight: bold;
  color: #d4990a;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  font-family: 'STKaiti', 'KaiTi', serif;
  margin-bottom: 8px;
}

.header-subtitle {
  font-size: 16px;
  color: #8fa4b8;
  font-family: 'STKaiti', 'KaiTi', serif;
}

.header-decoration {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.decoration-line {
  width: 60px;
  height: 1px;
  background: linear-gradient(90deg, transparent, #d4990a, transparent);
}

.decoration-symbol {
  color: #d4990a;
  font-size: 20px;
}

.stats-section {
  margin-bottom: 32px;
}

.stat-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  transition: all 0.3s ease;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}

.stat-icon {
  font-size: 32px;
  opacity: 0.8;
}

.disease-stat .stat-icon {
  color: #ff7875;
}
.accident-stat .stat-icon {
  color: #ffa940;
}
.natural-stat .stat-icon {
  color: #73d13d;
}
.other-stat .stat-icon {
  color: #40a9ff;
}

.stat-content {
  text-align: left;
}

.stat-title {
  font-size: 14px;
  color: #b3c5d1;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 2px;
}

.stat-label {
  font-size: 12px;
  color: #8fa4b8;
}

.main-content {
  margin-top: 32px;
}

.chart-card :deep(.ant-card-head),
.table-card :deep(.ant-card-head),
.warning-card :deep(.ant-card-head) {
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.05);
}

.chart-card :deep(.ant-card-head-title),
.table-card :deep(.ant-card-head-title),
.warning-card :deep(.ant-card-head-title) {
  color: #d4990a;
  font-weight: 600;
}

.chart-card :deep(.ant-card-body),
.table-card :deep(.ant-card-body),
.warning-card :deep(.ant-card-body) {
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border-radius: 0 0 8px 8px;
}

.chart-container {
  width: 100%;
  min-height: 400px;
  position: relative;
}

.chart-container > div {
  width: 100% !important;
  height: 100% !important;
}

.warning-list {
  max-height: 400px;
  overflow-y: auto;
}

.warning-item {
  padding: 16px;
  margin-bottom: 12px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  border-left: 4px solid;
}

.warning-item:nth-child(1) {
  border-left-color: #ff4d4f;
}
.warning-item:nth-child(2) {
  border-left-color: #faad14;
}
.warning-item:nth-child(3) {
  border-left-color: #1890ff;
}
.warning-item:nth-child(4) {
  border-left-color: #52c41a;
}

.warning-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.warning-level {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}

.warning-level.紧急 {
  background: #ff4d4f;
  color: #fff;
}
.warning-level.警告 {
  background: #faad14;
  color: #fff;
}
.warning-level.提醒 {
  background: #1890ff;
  color: #fff;
}
.warning-level.信息 {
  background: #52c41a;
  color: #fff;
}

.warning-title {
  color: #fff;
  font-weight: 600;
}

.warning-content {
  color: #b3c5d1;
  font-size: 14px;
  margin-bottom: 8px;
}

.warning-time {
  color: #8fa4b8;
  font-size: 12px;
}

:deep(.ant-table-thead > tr > th) {
  background: rgba(255, 255, 255, 0.1) !important;
  color: #d4990a !important;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

:deep(.ant-table-tbody > tr > td) {
  background: rgba(255, 255, 255, 0.05) !important;
  color: #fff !important;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

:deep(.ant-table-tbody > tr:hover > td) {
  background: rgba(255, 255, 255, 0.1) !important;
}
</style>
